package cn.pfz.controller;


import cn.pfz.Utils.JsonResult;
import cn.pfz.Utils.ParamMap;
import cn.pfz.domain.*;
import cn.pfz.search.Service.*;
import cn.pfz.search.domain.DestinationEs;
import cn.pfz.search.domain.StrategyEs;
import cn.pfz.search.domain.TravelEs;
import cn.pfz.search.domain.UserInfoEs;
import cn.pfz.search.qo.SearchQueryObject;
import cn.pfz.search.qo.SearchVo;
import cn.pfz.service.IDestinationService;
import cn.pfz.service.IStrategyService;
import cn.pfz.service.ITravelService;
import cn.pfz.service.IUserinfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.List;

/**
 * @Author: 普发忠
 * @Date: 2021/10/20/10:12
 */
@Slf4j
@RestController
public class SearchController {
    @Autowired
    private  IStrategyService strategyService;
    @Autowired
    private IStrategyEsService strategyEsService;
    
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    IDestinationEsService destinationEsService;
    
    @Autowired
    private IUserinfoService userinfoService;
    @Autowired
    private IUserInfoEsService userinfoEsService;
    
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelEsService travelEsService;

    @Autowired
    private ISearchService searchService;
    
    @GetMapping("/q")
    public Object qo(SearchQueryObject qo) throws Exception {
        //处理字符集
        String kw= URLDecoder.decode(qo.getKeyword(),"UTF-8");
        qo.setKeyword(kw);
        switch(qo.getType()){
            case SearchQueryObject.TYPE_DEST: return this.searchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY: return this.searchStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL: return this.searchTravel(qo);
            case SearchQueryObject.TYPE_USER: return this.searchUser(qo);
            default: return this.searchAll(qo);
        }

    }
    
    private Object searchAll(SearchQueryObject qo) {
        SearchVo searchVo = new SearchVo();
        //用户
        Page<UserInfo> us = searchService.searchWithHighLight(UserInfoEs.INDEX_NAME,
                "_doc", UserInfo.class, qo, "info", "city");

        //游记
        Page<Travel> ts = searchService.searchWithHighLight(TravelEs.INDEX_NAME,
                "_doc", Travel.class, qo, "title", "summary");
        for (Travel travel : ts) {
            travel.setAuthor(userinfoService.getById(travel.getAuthorId()));
        }
        //攻略
        Page<Strategy> sts = searchService.searchWithHighLight(StrategyEs.INDEX_NAME,
                "_doc", Strategy.class, qo, "title", "sub_title", "summary");
        //目的地
        Page<Destination> ds = searchService.searchWithHighLight(DestinationEs.INDEX_NAME,
                "_doc", Destination.class, qo, "name", "info");
        //List<Destination> ds = destinationService.list(new QueryWrapper<Destination>().like("name", qo.getKeyword()));
        searchVo.setStrategys(sts.getContent());
        searchVo.setTravels(ts.getContent());
        searchVo.setDest(ds.getContent());
        searchVo.setUsers(us.getContent());
        HashedMap hashedMap = new HashedMap();
        hashedMap.put("result",searchVo);
        hashedMap.put("qo",qo);
        return JsonResult.success(hashedMap);
    }
    
    private Object searchUser(SearchQueryObject qo) {
//        List<UserInfo> list = userinfoService.list(new QueryWrapper<UserInfo>()
//                .like("info", qo.getKeyword()).or().like("city", qo.getKeyword()).last("limit 10"));
        Page<UserInfo> user = searchService.searchWithHighLight(UserInfoEs.INDEX_NAME,
                "_doc", UserInfo.class, qo, "info", "city");
        //返回结果
        HashedMap hashedMap = new HashedMap();
        hashedMap.put("page",user);
        hashedMap.put("qo",qo);
        return JsonResult.success(hashedMap);
    }
    
    private Object searchTravel(SearchQueryObject qo) {
//        List<Travel> list = travelService.list(new QueryWrapper<Travel>()
//                .like("title", qo.getKeyword()).or().like("summary", qo.getKeyword()).last("limit 10 "));
        Page<Travel> travels = searchService.searchWithHighLight(TravelEs.INDEX_NAME,
                "_doc", Travel.class, qo, "title", "summary");
        //返回结果
        HashedMap hashedMap = new HashedMap();
        hashedMap.put("page",travels);
        hashedMap.put("qo",qo);
        return JsonResult.success(hashedMap);
    }
    
    private Object searchStrategy(SearchQueryObject qo) {
//        List<Strategy> list = strategyService.list(new QueryWrapper<Strategy>()
//                .like("title", qo.getKeyword()).or().like("sub_title", qo.getKeyword()).or().like("summary", qo.getKeyword()).last("limit 10"));
        Page<Strategy> strategies = searchService.searchWithHighLight(StrategyEs.INDEX_NAME,
                "_doc", Strategy.class, qo, "title", "subTitle", "summary");
        HashedMap hashedMap = new HashedMap();
        hashedMap.put("page",strategies);
        hashedMap.put("qo",qo);
        return JsonResult.success(hashedMap);
    }
    
    private Object searchDest(SearchQueryObject qo) {
        //查询 MYSQL  找传入的key是不是合法
       Destination dest = destinationService.getOne(new QueryWrapper<Destination>().eq("name",qo.getKeyword()));
        SearchVo searchVo = new SearchVo();
        if (dest != null) {
            //有  查询该目的下所有的游记 攻略 用户信息
            List<Strategy> strategies = strategyService.list(new QueryWrapper<Strategy>().eq("dest_id", dest.getId()).last("limit 10"));
            List<Travel> travels = travelService.list(new QueryWrapper<Travel>().eq("dest_id",dest.getId()));
            List<UserInfo> userInfos = userinfoService.list(new QueryWrapper<UserInfo>().eq("city", dest.getName()).last("limit 10"));
            searchVo.setStrategys(strategies);
            for (Travel travel : travels) {
                travel.setAuthor(userinfoService.getById(travel.getAuthorId()));
            }
            searchVo.setTravels(travels);
            searchVo.setUsers(userInfos);
            searchVo.setTotal(strategies.size()+travels.size()+userInfos.size()+0l);
        }
//        HashedMap hashedMap = new HashedMap();
//        hashedMap.put("result",searchVo);
//        hashedMap.put("qo",qo);
//        hashedMap.put("dest",dest);
        ParamMap put = new ParamMap().put("result", searchVo).put("qo", qo).put("dest", dest);
        return JsonResult.success(put);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    @GetMapping("/data")
    public Object data(){
        List<Strategy> list = strategyService.list();
        for (Strategy strategy : list) {
            StrategyEs strategyEs = new StrategyEs();
            BeanUtils.copyProperties(strategy, strategyEs);
            strategyEsService.save(strategyEs);
        }
        for (Destination destination : destinationService.list()) {
            if (destination.getInfo()==null){
                destination.setInfo("");
            }
            DestinationEs destinationEs = new DestinationEs();
            BeanUtils.copyProperties(destination, destinationEs);
            destinationEsService.save(destinationEs);
        }
    
        for (UserInfo userInfo : userinfoService.list()) {
            UserInfoEs userInfoEs = new UserInfoEs();
            BeanUtils.copyProperties(userInfo,userInfoEs);
            userinfoEsService.save(userInfoEs);
        }
    
        for (Travel travel : travelService.list()) {
            TravelEs travelEs = new TravelEs();
            BeanUtils.copyProperties(travel,travelEs);
            travelEsService.save(travelEs);
        }
        
        return " 1";
    }
    

}
